package com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle;

import com.google.gson.Gson;
import com.lkm.frame.P;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m_imple.steelyardserver.WeightChangeHelp;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsReceiverDataHandle;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.ICMDHandle;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;



public class CMDHistoryPrintHandle implements ICMDHandle {

    SteelyardDataModule mSteelyardDataModule;
    SteelyardServerModule mSteelyardServerModule;

    public CMDHistoryPrintHandle() {
        this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        this.mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
    }

    @Override
    public AbsReceiverDataHandle handle(CMDEntity cmdEntity) {

        if(this.mSteelyardDataModule==null)
            this.mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);

        if(this.mSteelyardServerModule==null)
            this.mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);

        SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(cmdEntity.getSteelyardMac());

        WeightChangeInfo weightChangeInfo= WeightChangeHelp.createByData(steelyardInfo, cmdEntity.getNum(),cmdEntity.getData());
        if(weightChangeInfo==null){
            P.p("======收到的weightChangeInfo为null");
            return null;
        }


        if(P.isDebug()) {
            String logs=cmdEntity.getSteelyardMac() + new Gson().toJson(weightChangeInfo);
            P.p("======CMD称重打印历史：" +logs );
            if(UIP.isPrint()){
                UIP.p("CMD打印历史："+logs,cmdEntity.getSteelyardMac());
            }
        }


        weightChangeInfo.setHistory(true);
        weightChangeInfo.isUseThisDate = true;

        
        if(weightChangeInfo.getWeight()==0){
            
            weightChangeInfo.setWeight(1);
            weightChangeInfo.setUnitPrice(weightChangeInfo.getTotalPrice());

        }

        //        SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(cmdEntity.getSteelyardMac());

        
        if(cmdEntity.is32Zero()){
            
            mSteelyardDataModule.print(cmdEntity.getSteelyardMac(),false);

        }else {
            
            mSteelyardDataModule.weightChange(cmdEntity.getSteelyardMac(), weightChangeInfo);

        }

//        OrderEntity orderEntity = mSteelyardDataModule.print(cmdEntity.getSteelyardMac());

        mSteelyardServerModule.tickOK_block(cmdEntity.getSteelyardMac());
        return null;
    }

}
